package com.jinzhi.jzweb.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.jzweb.dao.BannerDao;
import com.jinzhi.jzweb.domain.BannerDO;
import com.jinzhi.jzweb.service.BannerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;


/***
 * @author C
 * @Date 2023/12/5
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class BannerServiceImpl extends CoreServiceImpl<BannerDao, BannerDO> implements BannerService {

    private static final Integer num = 1;

    /**
     * 分页查询
     *
     * @param current
     * @param number
     * @return
     */
    @Override
    public Page<BannerDO> selectListPage(int current, int number) {
        // 校验参数
        if (current <= 0 && number <= 0) {
            current = 1;
            number = 10;
        }
        Page<BannerDO> page = new Page<>(current, number);
        EntityWrapper<BannerDO> wrapper = new EntityWrapper<>();
        wrapper.orderBy(true,"level",false).eq("is_deleted",0);
        wrapper.orderBy("create_date",true);
        return this.selectPage(page, wrapper);
    }

    /**
     * 新增banner
     *
     * @return
     */
    @Override
    public boolean insertBanner(BannerDO bannerDo) {
        // 校验
        if (bannerDo == null) {
            throw new RuntimeException("请输入参数");
        }

        return this.insert(bannerDo);
    }

    /**
     * 根据id查询banner
     *
     * @param id
     * @return
     */
    @Override
    public BannerDO selectBanner(Long id) {
        if (id <= 0) {
            throw new RuntimeException("参数异常");
        }
        EntityWrapper<BannerDO> wrapper = new EntityWrapper<>();
        wrapper.eq("id",id).eq("is_deleted",0);

        return this.selectOne(wrapper);
    }

    /**
     * 修改banner
     *
     * @return
     */
    @Override
    public boolean updateBanner(BannerDO bannerDO) {
        EntityWrapper<BannerDO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted",0);
        if (bannerDO.getId()!=null){
            wrapper.eq("id",bannerDO.getId());
        }
        return this.update(bannerDO,wrapper);
    }

    /**
     * 删除banner
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteBanner(Long id) {
        // 参数校验
        if (id <= 0) {
            throw new RuntimeException("参数异常");
        }
        // 根据id查询
        BannerDO bannerDO = this.selectById(id);
        if (bannerDO==null){
            throw new RuntimeException("删除失败，不存在该数据");
        }
        // 修改逻辑删除
        bannerDO.setDeleted(num);

        return this.updateById(bannerDO);
    }

    /***
     * 查询logo- 最新的一条数据
     * @return
     */
    @Override
    public BannerDO selectLogo() {
        EntityWrapper<BannerDO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("type",2);
        wrapper.orderBy("level",true);
        wrapper.orderBy("update_date",false).last("limit 1");
        BannerDO bannerDO = this.selectOne(wrapper);
        // 校验
        if (bannerDO == null) {
            return null;
        }
        // 查询
        return bannerDO;
    }

    @Override
    public BannerDO selectSchoolBadge() {
        EntityWrapper<BannerDO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("type",2);
        wrapper.eq("name","校徽");
        wrapper.orderBy("level",true);
        wrapper.orderBy("update_date",false).last("limit 1");
        BannerDO bannerDO = this.selectOne(wrapper);
        // 校验
        if (bannerDO == null) {
            return null;
        }
        // 查询
        return bannerDO;
    }


}
